﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SportsStore.Models;
using Microsoft.EntityFrameworkCore;

namespace SportsStore.Data.Implementation
{
    public class EfOrderRepository : IOrderRepository
    {
        private ApplicationDbContext _context;
        public EfOrderRepository(ApplicationDbContext ctx)
        {
            _context = ctx;
        }
        public IQueryable<Order> Orders => _context.Orders.Include(o=>o.Lines).ThenInclude(l=>l.Product);

        public void SaveOrder(Order order)
        {
            _context.AttachRange(order.Lines.Select(l => l.Product));
            if (order.Id == 0)
            {
                _context.Orders.Add(order);
            }
            _context.SaveChanges();
        }
    }
}
